package com.asimplefox.controllers;


import com.asimplefox.entity.VidHistory;
import com.asimplefox.filter.RedirectFilter;
import com.asimplefox.serurity.UserRoleDetails;
import com.asimplefox.entity.vo.UserReVo;
import com.asimplefox.service.IRoleService;
import com.asimplefox.service.IUserService;
import com.asimplefox.service.IVidHistoryService;
import com.asimplefox.service.abst.ResultU;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kloping
 * @since 2024-10-14
 */
@RestController
@RequestMapping("/user")
public class UserController implements ResultU {

    @RequestMapping("/info")
    @PreAuthorize("hasAuthority('user')")
    public Object info(@AuthenticationPrincipal UserRoleDetails userDetails) {
        return userDetails;
    }

    @Autowired
    IUserService userService;

    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('admin','system')")
    public Object list(@RequestParam(value = "ps", defaultValue = "8") Integer pageSize,
                       @RequestParam(value = "pn", defaultValue = "1") Integer pageNum) {
        return userService.list(pageSize, pageNum);
    }

    @RequestMapping("/del")
    @PreAuthorize("hasAnyAuthority('admin','system')")
    public Object del(@RequestParam("id") String id) {
        return userService.removeById(id);
    }

    @Autowired
    IRoleService roleService;

    @GetMapping("/role")
    @PreAuthorize("hasAuthority('system')")
    public Object role(@RequestParam("id") Integer id) {
        return roleService.role(id);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('admin','system')")
    public Object add(@RequestBody UserReVo userVo, HttpServletResponse rsp) {
        return userService.addUser(userVo, rsp);
    }

    @PostMapping("/modify")
    @PreAuthorize("hasAnyAuthority('admin','user')")
    public Object modify(@AuthenticationPrincipal UserRoleDetails userDetails,
                         @RequestParam("id") @Nullable Integer id, @RequestParam("avatar") @Nullable MultipartFile avatar,
                         @RequestParam("nickname") @Nullable String nickname, HttpServletResponse rsp) {
        return userService.modify(id, avatar, nickname, rsp, userDetails);
    }

    @RequestMapping("/list/admins")
    @PreAuthorize("hasAnyAuthority('admin','system')")
    public Object listAdmins() {
        return userService.listAdmins();
    }

    @Autowired
    IVidHistoryService vidHistoryService;

    @RequestMapping("/vid/history")
    @PreAuthorize("hasAnyAuthority('user')")
    public Object listHistory(@AuthenticationPrincipal UserRoleDetails details) {
        List<VidHistory> list = vidHistoryService.lambdaQuery()
                .eq(VidHistory::getUid, details.getUser().getId())
                .orderByDesc(VidHistory::getTime)
                .list();
        list.forEach(vidHistory ->
                vidHistory.setTimestamp(RedirectFilter.SF_0.format(vidHistory.getTime())));
        return list;
    }
}
